﻿package config;

import org.json.simple.JSONObject;

import util.KJsonObject;
import util.KPrintInfo;

public class ServerConfig {
	static private ServerConfig configs;

	private JSONObject jsonObject;

	private int server_port;

	private String dbHost;
	private String dbPort;
	private String dbName;
	private String dbuserName;
	private String dbpsw;

	static public ServerConfig getInstance() {
		if (configs == null) {
			configs = new ServerConfig();
		}
		return configs;
	}

	public void init() {
		if (loadConfing()) {
			KPrintInfo.println("-------加载server___mysql配置文件成功-----");
		} else {
			KPrintInfo.println("-------加载server___mysql配置文件失败-----");
			return;
		}
	}

	public Boolean loadConfing() {
		LoadFile loadFile = new LoadFile();
		String fileString = loadFile.Load("config/serverConfig");
		if (fileString != "") {
			jsonObject = KJsonObject.decode(fileString);
			return initConfigData();
		}
		return false;
	}

	private Boolean initConfigData() {
		server_port = intChange(jsonObject.get("server_port"));

		dbHost = (String) jsonObject.get("mysql_ip");
		dbPort = (String) jsonObject.get("mysql_port");
		dbuserName = (String) jsonObject.get("dbuserName");
		dbpsw = (String) jsonObject.get("db_password");

		dbName = (String) jsonObject.get("dbName");

		return true;
	}

	/**
	 * 转成int 类型
	 * 
	 * @param obj
	 * @return
	 */
	private int intChange(Object obj) {
		return Integer.parseInt(String.valueOf(obj));
	}

	public String getDbHost() {
		return dbHost;
	}

	public void setDbHost(String dbHost) {
		this.dbHost = dbHost;
	}

	public String getDbPort() {
		return dbPort;
	}

	public void setDbPort(String dbPort) {
		this.dbPort = dbPort;
	}

	public String getDbName() {
		return dbName;
	}

	public void setDbName(String dbName) {
		this.dbName = dbName;
	}

	public String getDbuserName() {
		return dbuserName;
	}

	public void setDbuserName(String dbuserName) {
		this.dbuserName = dbuserName;
	}

	public String getDbpsw() {
		return dbpsw;
	}

	public void setDbpsw(String dbpsw) {
		this.dbpsw = dbpsw;
	}

	public int getServer_port() {
		return server_port;
	}

	public void setServer_port(int server_port) {
		this.server_port = server_port;
	}
}
